<template>
  <div>
    <!-- 修改页面 -->
    <div class="settitle">
      <div class="setimg" @click="$router.back()">
        <img src="../assets/imgs/iconse/左箭头.png" alt="" />
      </div>
      <div class="text">
        <h2>{{ runlist.title }}</h2>
        <span @click="setlist">保存</span>
      </div>
    </div>

    <div class="item">
      <div class="text">
        <div class="box" v-for="(item, index) in runlist.uers" :key="index">
          <p>{{ item.name }}</p>
          <div class="sp" v-if="item.got">
            <span
              @click="egg(ind, item, index)"
              v-for="(item, ind) in item.values"
              :key="ind"
              >{{ item }}</span
            >
          </div>
          <span @click="getname(index)" v-if="!item.got">{{
            item.values
          }}</span>
        </div>
      </div>
    </div>

    <input
      type="text"
      class="edit"
      v-model="val"
      v-show="show"
      @keydown.enter="add(val)"
      @keydown.esc="esc"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      runlist: null,
      val: "",
      show: false,
      ids: 0,
      intos: 0,
    };
  },
  methods: {
    setlist() {
      // JSON.parse(list)是把字符串转数组 存的时候转字符串，取就转对象
      let oldlist = JSON.parse(localStorage.getItem("keys"));
      let index = localStorage.getItem("index");
      if(index==0){
        localStorage.setItem("basic","set")
      }

      //修改卡片
      oldlist[index].flag = true;
      // 将修改值写入旧数组中
      console.log(this.runlist.uers);
      oldlist[index].basic[0].uers = this.runlist.uers;
      console.log("oldlist",oldlist);

      let newlist = oldlist.filter((item) => item.flag);
      localStorage.setItem("newlist", JSON.stringify(newlist));
      localStorage.setItem("keys", JSON.stringify(oldlist));
      localStorage.setItem("oniu", 1);
      this.$router.push("creator");
    },
    getlist() {
      let index = localStorage.getItem("index");
      let oldlist = JSON.parse(localStorage.getItem("keys"));
      this.runlist = oldlist[index].basic[0];
      console.log(this.runlist);
    },
    getname(index) {
      this.show = true;
      this.ids = index;
      this.val = this.runlist.uers[this.ids].values;
    },
    add(val) {
      console.log(val);
      if (!(this.ids == 4)) {
        this.runlist.uers[this.ids].values = val;
        this.val = "";
        this.show = false;
      } else if (this.ids == 4) {
        this.runlist.uers[this.ids].values[this.intos] = val;
        this.val = "";
        this.show = false;
      }
    },
    egg(ind, item, index) {
      this.ids = index;
      this.val = item;
      this.intos = ind;
      this.show = true;
    },
    esc() {
      this.val = "";
      this.show = false;
    },
  },
  created() {
    this.getlist();
  },
};
</script>

<style lang="scss" scoped>
// 输入框
.edit {
  position: absolute;
  width: 80%;
  height: 40px;
  right: 0px;
  left: 0px;
  margin: 50px auto;
  background-color: #fff;
  font-size: 22px;
  text-indent: 1em;
  border: 0px;
  border-radius: 20px;
  box-shadow: 0px 0px 8px #ccc;
}

// 标题
.settitle {
  height: 60px;
  display: flex;
  align-items: center;
  background-color: #fff;
  position: relative;

  box-shadow: 0px 0px 0px 1px rgba($color: #cccc, $alpha: 0.4);
  .setimg {
    height: 18px;
    position: absolute;
    left: 15px;
    img {
      height: 100%;
      display: block;
    }
  }
  .text {
    margin: 0 auto;
    span {
      display: block;
      width: 50px;
      height: 30px;
      line-height: 30px;
      font-size: 15px;
      color: #1a73e8;
      text-align: center;
      position: absolute;
      top: 0px;
      bottom: 0px;
      right: 20px;
      margin: auto 0;
    }
  }
}

// 数据列表
.item {
  box-sizing: border-box;
  padding: 0px 20px;
  margin-top: 20px;
  .text {
    .box {
      height: 70px;
      margin-bottom: 16px;
      p {
        text-indent: 10px;
        color: #333;
      }
      span {
        border-bottom: 1px solid #ccc;
        height: 50px;
        line-height: 50px;
        font-size: 19px;
        text-indent: 10px;
        display: block;
        position: relative;
        &::after {
          content: "";
          display: block;
          position: absolute;
          height: 26px;
          width: 13px;
          background: url(../assets/imgs/右箭头.png) no-repeat center;
          background-size: 100%;
          top: 0px;
          bottom: 0px;
          right: 0px;
          margin: auto 0px;
        }
      }
    }
    span {
      border-bottom: 1px solid #ccc;
      height: 70px;
      line-height: 70px;
      font-size: 19px;
      text-indent: 10px;
      display: block;
      margin-bottom: 10px;
      position: relative;
      &::after {
        content: "";
        display: block;
        position: absolute;
        height: 26px;
        width: 13px;
        background: url(../assets/imgs/右箭头.png) no-repeat center;
        background-size: 100%;
        top: 0px;
        bottom: 0px;
        right: 0px;
        margin: auto 0px;
      }
    }
  }
}
</style>